package com.cqtd.base.action;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cqtd.base.pojo.FooJob;
import com.cqtd.util.FooUtils;

@Controller
@RequestMapping("/system/dictAction")
public class DictAction extends FooGenericAction {
	@RequestMapping("/testQuartzJob")
	@ResponseBody
	public void testQuartzJob(HttpServletRequest request,
			HttpServletResponse response) throws IOException,
			SchedulerException {
		// define the job and tie it to our HelloJob class
		SchedulerFactory schedFact = (SchedulerFactory) request.getSession()
				.getServletContext()
				.getAttribute("org.quartz.impl.StdSchedulerFactory.KEY");
		FooUtils.log(schedFact.getScheduler().isStarted());

		JobDataMap jobDataMap = new JobDataMap();
		jobDataMap.put("id", "myJobPk");
		JobDetail jobDetail = JobBuilder.newJob(FooJob.class)
				.withIdentity("myJob", "group1").usingJobData(jobDataMap)
				.build();

		Trigger trigger = TriggerBuilder
				.newTrigger()
				.withIdentity("trigger3", "group1")
				.withSchedule(
						CronScheduleBuilder.cronSchedule("0/10 * * * * ?"))
				.forJob(jobDetail).build();

		Scheduler scheduler = schedFact.getScheduler();
		scheduler.deleteJob(new JobKey("myJob", "group1"));
		scheduler.scheduleJob(jobDetail, trigger);
	}
}
